#include <stdio.h>
#define N 4
#define M 10
static int value(int a[], int i, int j)
{
    if (i >= j)
        return a[(i * (i + 1)) / 2 + j];
    else
        return a[(j * (j + 1)) / 2 + i];
}
 
static void madd(int a[], int b[], int c[][N])
{
    int i, j;
    for (i = 0; i < N; i++)
    {
        for (j = 0; j < N; j++)
        {
            c[i][j] = value(a, i, j) + value(b, i, j);
        }
    }
}
 
static void mult(int a[], int b[], int c[][N])
{
    int i, j, k, sum;
    for (i = 0; i < N; i++)
    {
        for (j = 0; j < N; j++)
        {
            sum = 0;
            for (k = 0; k < N; k++)
            {
                sum = sum + value(a, i, k) * value(b, k, j);
            }
            c[i][j] = sum;
        }
    }
}
 
static void disp1(int a[])
{
    int i, j;
    for (i = 0; i < N; i++)
    {
        for (j = 0; j < N; j++)
        {
            printf("%4d", value(a, i, j));
        }
        printf("\n");
    }
}
 
static void disp2(int c[][N])
{
    int i, j;
 
    for (i = 0; i < N; i++)
    {
        for (j = 0; j < N; j++)
        {
            printf("%4d", c[i][j]);
        }
        printf("\n");
    }
}
 
int main(int argc, char* argv[])
{
    int a[M] = { 0, 1, 0, 1, 1, 0, 1, 1, 1, 0 };
    int b[M] = { 1, 3, 6, 6, 10, 15, 10, 15, 21, 28 };
    int c1[N][N], c2[N][N];
    printf("a矩阵:\n");
    disp1(a);
    printf("b矩阵:\n");
    disp1(b);
 
    madd(a, b, c1);
    printf("a+b:\n");
    disp2(c1);
 
    mult(a, b, c2);
    printf("a*b:\n");
    disp2(c2);
    return 0;
}
